import 'package:business_user/logic/user_logic.dart';
import 'package:business_user/routes/user_routes.dart';
import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_ui/common/theme/theme_controller.dart';
import 'package:lib_ui/utils/ld_font.dart';

class MainPageDrawer extends StatelessWidget {
  MainPageDrawer({super.key});

  final currentLocale = Get.locale;
  final themeController = Get.find<ThemeController>();
  late final userLogic = Get.put(UserLogic());

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.zero,
      children: [
        DrawerHeader(
          child: _buildUserInfoDrawer(),
        ),
        Obx(() {
          return ListTile(
            title: Text(MainPageTranslation.changeTheme.tr),
            trailing: Switch(
                value: themeController.isDarkMode.value,
                onChanged: (value) {
                  themeController.toggleTheme();
                }),
          );
        }),
        ListTile(
          title: Text(MainPageTranslation.changeLanguage.tr),
          trailing: Switch(
            value: Get.locale == const Locale('zh', 'CN'),
            onChanged: (value) {
              final currentLocale = Get.locale;
              if (currentLocale == const Locale('zh', 'CN')) {
                Get.updateLocale(const Locale('en', 'US'));
              } else {
                Get.updateLocale(const Locale('zh', 'CN'));
              }
            },
          ),
        ),
      ],
    );
  }

  _buildUserInfoDrawer() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Center(
          child: SizedBox(
            width: 50,
            height: 50,
            child: ClipOval(
              child: Container(
                color: Colors.grey,
                child: const Icon(Icons.person),
              ),
            ),
          ),
        ),
        Center(
          child: Obx(() {
            if (AuthService.to.user.value == null) {
              return Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    GestureDetector(onTap: () => UserRoutes.login.push(), child: Text(MainPageTranslation.login.tr, style: LDFont.regular())),
                    const SizedBox(width: 15),
                    GestureDetector(onTap: () => UserRoutes.register.push(), child: Text(MainPageTranslation.register.tr, style: LDFont.regular())),
                  ],
                ),
              );
            } else {
              return Container(
                padding: const EdgeInsets.symmetric(vertical: 10),
                alignment: Alignment.center,
                child: Column(
                  children: [
                    Text(
                      AuthService.to.user.value!.username,
                      style: LDFont.regular(),
                    ),
                    GestureDetector(
                      onTap: () async {
                        await userLogic.logout();
                        AuthService.to.logout(() {});
                      },
                      child: Text(MainPageTranslation.register.tr, style: LDFont.regular()),
                    )
                  ],
                ),
              );
            }
          }),
        )
      ],
    );
  }

  _commonItem({required Widget tile, required Widget end}) {
    return Expanded(
      child: SizedBox(
        height: 40,
        child: Row(
          children: [
            tile,
            Expanded(child: Container()),
            end,
          ],
        ),
      ),
    );
  }

  _buildChangeLanguage() {
    return _commonItem(
      tile: Obx(() {
        return Text('当前语言-${currentLocale == const Locale('zh', 'CN') ? "中文" : "英文"}', style: LDFont.regular());
      }),
      end: Switch(
        value: currentLocale == const Locale('zh', 'CN'),
        onChanged: (value) {
          if (currentLocale == const Locale('zh', 'CN')) {
            Get.updateLocale(const Locale('en', 'US'));
          } else {
            Get.updateLocale(const Locale('zh', 'CN'));
          }
        },
      ),
    );
  }
}
